import { request } from './axios.js'

// LeanCloud用户相关API
export const userAPI = {
    // 用户注册
    register(userData) {
        return request.post('/users', userData)
    },

    // 用户登录
    login(username, password) {
        return request.post('/login', {
            username,
            password,
        })
    },

    // 获取当前用户信息
    getCurrentUser(sessionToken) {
        return request.get('/users/me', {
            headers: {
                'X-LC-Session': sessionToken,
            },
        })
    },

    // 更新用户信息
    updateUser(userId, userData, sessionToken) {
        return request.put(`/users/${userId}`, userData, {
            headers: {
                'X-LC-Session': sessionToken,
            },
        })
    },

    // 重置密码（需要验证邮箱）
    resetPassword(email) {
        return request.post('/requestPasswordReset', { email })
    },

    // 退出登录
    logout(sessionToken) {
        return request.post(
            '/logout',
            {},
            {
                headers: {
                    'X-LC-Session': sessionToken,
                },
            },
        )
    },
}

// LeanCloud数据存储相关API
export const dataAPI = {
    // 创建对象
    createObject(className, data, sessionToken = '') {
        const headers = sessionToken ? { 'X-LC-Session': sessionToken } : {}
        return request.post(`/classes/${className}`, data, { headers })
    },

    // 查询对象
    queryObjects(className, where = {}, sessionToken = '') {
        const headers = sessionToken ? { 'X-LC-Session': sessionToken } : {}
        return request.get(`/classes/${className}`, {
            params: { where: JSON.stringify(where) },
            headers,
        })
    },

    // 更新对象
    updateObject(className, objectId, data, sessionToken = '') {
        const headers = sessionToken ? { 'X-LC-Session': sessionToken } : {}
        return request.put(`/classes/${className}/${objectId}`, data, { headers })
    },

    // 删除对象
    deleteObject(className, objectId, sessionToken = '') {
        const headers = sessionToken ? { 'X-LC-Session': sessionToken } : {}
        return request.delete(`/classes/${className}/${objectId}`, { headers })
    },
}

export default {
    userAPI,
    dataAPI,
}
